package job;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.aton.db.Config;

import play.cache.Cache;
import play.jobs.Job;
import services.SystemParamService;
import utils.BizConstants;
import utils.ShellUtil;

/**
 * 轮询优化状态任务
 * @author wangshaoming
 *
 */
public class MajorJob extends Job {
	@Override
	public void doJob() throws IOException{
		String majorStatus=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_MAJOR_STSTUS_FILE"));
		String majorip=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_IP"));
		String majoruser=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_USER"));
		String majorpass=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_PASS"));
		//循环解析log中的状态放到Cache中方便前面查询
		while(true){
			//执行查询状态脚本
			String shellTxt="cd "+majorStatus+"&& ./get_msg.sh";
			Map<String,Object> shellMap=ShellUtil.execAndPass(majorip,majoruser,majorpass,shellTxt);
			if(shellMap!=null){
				int status=(int)shellMap.get("status");
				if(status==0){
					//获取脚本打印的值
					String outPut=(String)shellMap.get("outPut");
					if(outPut.contains("Success")){
						//将临时文件删除
						File file=new File(BizConstants.MAJORFILE_TEMP);
						if(file.exists()){
							file.delete();
						}
						//将优化状态置为1，表示已完成
						Cache.set(BizConstants.MAJORKEYWORDS_STATUS, 1);
						break;
					}
				}
			}
		}
		return;
	}
		
}
